import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import {
  Row,
  Col,
  Card,
  Form,
  Input,
  Select,
  Icon,
  Button,
  message,
  DatePicker,
  Modal,
  Divider,
  Table,
} from 'antd';
import PageHeaderLayout from '../../../layouts/PageHeaderLayout';
import { Link } from 'dva/router';
import styles from './style.less';

const FormItem = Form.Item;
const statusArray = [
  '未上报',
  '等待审批',
  '审批进行中',
  '审批通过',
  '审批拒绝',
  '已放款未补录',
  '已放款已补录',
  '已放款已确认',
];

const { Option } = Select;
const { MonthPicker, RangePicker } = DatePicker;


@connect(({ supplement }) => ({
  supplement,
}))
@Form.create()
export default class BusinessImport extends PureComponent {
  state = {
    createTimeBegin: null,
    createTimeEnd: null,
    formValues: null,
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'supplement/fetchlImportList',
      payload: { offset: 0, limit: 10 },
    });
  }

  handleStandardTableChange = e => {
    const { dispatch, form } = this.props;
    form.validateFields((err, fieldsValue) => {
      if (err) return;
      const values = {
        offset: (e.current - 1) * e.pageSize,
        limit: e.pageSize,
        ...fieldsValue,
        createTimeBegin: this.state.createTimeBegin,
        createTimeEnd: this.state.createTimeEnd,
      };
      this.setState({
        formValues: values,
      });
      dispatch({
        type: 'supplement/fetchlImportList',
        payload: values,
      });
    });
  };

  handleChangeTime = (dates, dateString) => {
    // console.log("dates", dates)
    // console.log("dateString", dateString)
    this.setState({
      createTimeBegin: dates[0],
      createTimeEnd: dates[1],
    });
  };

  //查询产品
  handleSearch = e => {
    e.preventDefault();
    const { dispatch, form } = this.props;
    form.validateFields((err, fieldsValue) => {
      if (err) return;
      const values = {
        offset: 0,
        limit: 10,
        ...fieldsValue,
        createTimeBegin: this.state.createTimeBegin,
        createTimeEnd: this.state.createTimeEnd,
      };
      this.setState({
        formValues: values,
      });
      dispatch({
        type: 'supplement/fetchlImportList',
        payload: values,
      });
    });
  };

  //重置
  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    this.setState({
      formValues: {},
    });
    dispatch({
      type: 'supplement/fetchlImportList',
      payload: {
        offset: 0,
        limit: 10,
      },
    });
  };

  renderAdvancedForm() {
    const { getFieldDecorator } = this.props.form;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
            <FormItem label="客户名称">
              {getFieldDecorator('aname')(<Input placeholder="请输入" />)}
            </FormItem>
          </Col>

          <Col md={8} sm={24}>
            <FormItem label="手机号">
              {getFieldDecorator('aphone')(<Input placeholder="请输入" />)}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <FormItem label="申请编号">
              {getFieldDecorator('tradeNo')(<Input placeholder="请输入" />)}
            </FormItem>
          </Col>
        </Row>
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
            <FormItem label="创建时间">
              {getFieldDecorator('createTimeBegins')(
                <RangePicker
                  allowClear={true}
                  showToday={true}
                  onChange={this.handleChangeTime}
                  style={{ width: '100%' }}
                  format="YYYY-MM-DD"
                />
              )}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <FormItem label="状态">
              {getFieldDecorator('status')(
                <Select placeholder="请选择">
                  <Option value={'0'}>未上报</Option>
                  <Option value={'10'}>等待审批</Option>
                  <Option value={'11'}>审批进行中</Option>
                  <Option value={'12'}>审批通过</Option>
                  <Option value={'13'}>审批拒绝</Option>
                  <Option value={'14'}>已放款未补录</Option>
                  <Option value={'15'}>已放款已补录</Option>
                  <Option value={'16'}>已放款已确认</Option>
                </Select>
              )}
            </FormItem>
          </Col>
          <span className={styles.submitButtons} style={{ textAlign: 'right' }}>
            <Button type="primary" htmlType="submit">
              查询
            </Button>
            <Button type="normal" onClick={this.handleFormReset} style={{ marginLeft: 10 }}>
              重置
            </Button>
          </span>
        </Row>
      </Form>
    );
  }

  columns = [
    {
      title: '申请编号',
      dataIndex: 'tradeNo',
      key: 'tradeNo',
      align: 'center',
    },
    {
      title: '客户名称',
      dataIndex: 'aname',
      key: 'aname',
      align: 'center',
    },
    {
      title: '手机号',
      dataIndex: 'aphone',
      key: 'aphone',
      align: 'center',
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      align: 'center',
      render(val) {
        switch (val) {
          case '0':
            return '未上报';
          case '10':
            return '等待审批';
          case '11':
            return '审批进行中';
          case '12':
            return '审批通过';
          case '13':
            return '审批拒绝';
          case '14':
            return '已放款未补录';
          case '15':
            return '已放款已补录';
          case '16':
            return '已放款已确认';
        }
      },
    },
    {
      title: '操作',
      dataIndex: 'id',
      align: 'center',
      render: (text, record) => (
        <Fragment>
          {/* <Link to="">详情</Link> */}
          <Link to={`/apply/businessDetails?id=${record.id}&status=${record.status}`}>详情</Link>
          <Divider type="vertical" />
          {record.status != 0 ? (
            <a onClick={() => this.handleQueryStatus(record)}>
              查询状态<Divider type="vertical" />
            </a>
          ) : null}
          <a onClick={() => this.handleSubmitBrank(record)}>重新上报</a>
        </Fragment>
      ),
    },
  ];

  handleSubmitBrank = record => {
    Modal.confirm({
      title: `是否将此申请作废，然后使用此资料创建一笔新的申请，可以修改后再次上报银行，此操作务必告知银行当前申请已作废！`,
      okText: '确认',
      okType: 'danger',
      cancelText: '取消',
      onOk: () => {
        const { dispatch } = this.props;
        dispatch({
          type: 'supplement/againSubmitBrank',
          payload: { id: record.id },
          callback: () => {
            dispatch({
              type: 'supplement/fetchlImportList',
              payload: { offset: 0, limit: 10 },
            });
          },
        });
      },
    });
  };

  handleQueryStatus = record => {
    const { dispatch } = this.props;
    dispatch({
      type: 'supplement/queryBusinessStatus',
      payload: { id: record.id },
      callback: () => {
        dispatch({
          type: 'supplement/fetchlImportList',
          payload: { offset: 0, limit: 10 },
        });
      },
    });
  };

  render() {
    const { supplement: { businessList, businessLoading, businessPagination } } = this.props;
    const { dispatch } = this.props;
    const { getFieldDecorator } = this.props.form;

    return (
      <PageHeaderLayout title="业务补导入">
        {
          // JSON.stringify(productDetails)
        }
        <Card bordered={false}>
          <div className={styles.tableList}>
            <div className={styles.tableListForm}>{this.renderAdvancedForm()}</div>
            <Table
              dataSource={businessList}
              columns={this.columns}
              rowKey="id"
              pagination={businessPagination}
              loading={businessLoading}
              onChange={this.handleStandardTableChange}
            />
          </div>
        </Card>
      </PageHeaderLayout>
    );
  }
}
